package cn.itheima.utils;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtils {

	private static SessionFactory sessionFactory;
	static {
		// 1创建，调用空参构造
		Configuration conf = new Configuration();
		// 2读取指定配置文件=>空参加载方法，加载src下的hibernate.cfg.xml文件
		conf.configure();
		// 3根据配置信息，创建SessionFactory
		sessionFactory = conf.buildSessionFactory();
	}

	// 获得session => 获得全新session
	public static Session openSession() {
		// 4.获得session，打开一个session对象
		Session session = sessionFactory.openSession();

		return session;
	}

	// 获得session => 获得与线程绑定的session
	public static Session getCurrentSession() {

		// 4.获得session，打开一个session对象
		Session session = sessionFactory.getCurrentSession();

		return session;
	}
}
